<template>
	<view>
		<uni-card title="人员管理" mode="title" :is-shadow="true">
			<uni-grid :column="3" :showBorder="false" :square="false">
				<uni-grid-item>
					<view @click="selectUsers()">
						<view style="display: flex;align-items: center;flex-direction: column;">
							<image src="/static/icon/u1.png" style="width: 100rpx;height: 100rpx;"></image>
						</view>
						<view style="text-align: center;">部门成员</view>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-card>
		<uni-card title="工单管理" mode="title" :is-shadow="true">
			<uni-grid :column="3" :showBorder="false" :square="false">
				<uni-grid-item>
					<view @click="selectOrders()">
						<view style="display: flex;align-items: center;flex-direction: column;">
							<image src="/static/icon/gdcx.png" style="width: 100rpx;height: 100rpx;"></image>
						</view>
						<view style="text-align: center;">部门工单</view>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view @click="maintainOrders()">
						<uni-badge size="small" :text="maintainOrderNum" absolute="rightTop" type="warning">
							<view style="display: flex;align-items: center;flex-direction: column;">
								<image src="/static/icon/wxlb.png" style="width: 100rpx;height: 100rpx;"></image>
							</view>
							<view style="text-align: center;">维修完成</view>
						</uni-badge>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view @click="addOrder()">
						<view style="display: flex;align-items: center;flex-direction: column;">
							<image src="/static/icon/xjgd.png" style="width: 100rpx;height: 100rpx;"></image>
						</view>
						<view style="text-align: center;">新建工单</view>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-card>
		<uni-card title="设置" mode="title" :is-shadow="true">
			<uni-grid :column="3" :showBorder="false" :square="false">
				<uni-grid-item>
					<view @click="updatePassword()">
						<view style="display: flex;align-items: center;flex-direction: column;">
							<image src="/static/icon/lock.png" style="width: 100rpx;height: 100rpx;"></image>
						</view>
						<view style="text-align: center;">修改密码</view>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view @click="updatePhone()">
						<view style="display: flex;align-items: center;flex-direction: column;">
							<image src="/static/icon/phone.png" style="width: 100rpx;height: 100rpx;"></image>
						</view>
						<view style="text-align: center;">修改手机号</view>
					</view>
				</uni-grid-item>
			</uni-grid>
		</uni-card>
	</view>
</template>

<script>
	import utils from "../../../../utils.js"
	export default {
		data() {
			return {
				maintainOrderNum: null
			}
		},
		methods: {
			//人员管理
			selectUsers() {
				uni.navigateTo({
					url: "/packageC/pages/managerPage/people/deptUsers/deptUsers"
				})
			},
			//工单管理
			selectOrders() {
				uni.navigateTo({
					url: "/packageC/pages/managerPage/order/deptOrders/deptOrders",
				})
			},
			maintainOrders() {
				uni.navigateTo({
					url: "/packageD/pages/userPage/order/maintainOrders/maintainOrders",
				})
			},
			addOrder() {
				uni.navigateTo({
					url: "/packageD/pages/userPage/order/addOrder/addOrder",
				})
			},

			//设置
			updatePassword() {
				uni.navigateTo({
					url: "/pages/updatePassword/updatePassword"
				})
			},
			updatePhone() {
				uni.navigateTo({
					url: "/pages/updatePhone/updatePhone"
				})
			},
			selectMaintainOrder() {
				const that = this
				var token = getApp().globalData.token
				uni.request({
					url: getApp().globalData.BASE_URL + '/user/selectOrders',
					data: {
						token: token,
						status: '工人已完成',
					},
					header: {},
					success(response) {
						console.log(response)
						if (response.data.status === 444) {
							uni.showToast({
								title: '您的登录信息已过期，请重新登录',
								duration: 2000,
								icon: "none",
								success() {
									setTimeout(function() {
										uni.reLaunch({
											url: "/pages/login/login",
										})
									}, 2000)
								}
							});
						} else if (response.data.status === 445) {
							uni.showToast({
								title: '您没有此操作权限',
								duration: 2000,
								icon: "none"
							});
						} else {
							that.tableData = response.data.orders
							let newMaintainOrdersNum = response.data.orders.length
							utils.$emit("updateMaintainOrdersNum", newMaintainOrdersNum)
						}
					}
				})
			},
		},
		created() {
			console.log(getApp().globalData.user)
			if (getApp().globalData.user.openid == null || getApp().globalData.user.openid == "") {
				var that = this
				console.log("暂未绑定微信账号")
				uni.showModal({
					title: "绑定微信账号",
					content: "绑定后可接收工单状态通知",
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.showLoading({
								title: '正在绑定...'
							});
							wx.login({
								success(resCode) {
									console.log("res: ", resCode)
									if (resCode.code) {
										//发起网络请求
										uni.request({
											url: getApp().globalData.BASE_URL +
												'/user/bindOpenid',
											data: {
												code: resCode.code,
												token: getApp().globalData.token
											},
											header: {},
											success(response) {
												console.log("绑定结果:"
													,response)
												uni.hideLoading()
												if (response.data.status === 444) {
													uni.showToast({
														title: '您的登录信息已过期，请重新登录',
														duration: 2000,
														icon: "none",
														success() {
															setTimeout(function() {
																uni.reLaunch({
																	url: "/pages/login/login",
																})
															}, 2000)
														}
													});
												} else if (response.data.status === 445) {
													uni.showToast({
														title: '您没有此操作权限',
														duration: 2000,
														icon: "none"
													});
												} else if (response.data.openid != null ||
													response.data.user.openid != "") {
													uni.showToast({
														title: '绑定成功',
														duration: 2000,
														icon: "success"
													});
													getApp().globalData.user = response.data
													console.log(getApp().globalData.user)
												} else {
													uni.showToast({
														title: '绑定失败',
														duration: 2000,
														icon: "none"
													});
												}
											}
										})
									} else {
										uni.hideLoading()
										uni.showToast({
											title: '绑定失败',
											duration: 2000,
											icon: "none"
										});
									}
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
		},
		mounted() {
			var that = this
			utils.$on('updateMaintainOrdersNum', function(newMaintainOrdersNum) {
				console.log("updateMaintainOrdersNum:", newMaintainOrdersNum)
				if (newMaintainOrdersNum > 0) {
					that.maintainOrderNum = newMaintainOrdersNum
				} else {
					that.maintainOrderNum = null
				}
			})
			this.selectMaintainOrder()
		}
	}
</script>

<style>

</style>
